/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the GPL
 * version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs.wizards;

import org.digitalsoul.switchplugin.actions.ActionType;
import org.digitalsoul.switchplugin.prefs.CriteriaListUI;
import org.digitalsoul.switchplugin.prefs.util.HelpTexts;
import org.digitalsoul.switchplugin.prefs.util.ItemListConfigurator;
import org.digitalsoul.switchplugin.prefs.util.UITools;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class ActionWizard extends CriteriaWizard {

    private Text text2;
    private ActionType type;

    public ActionWizard(ItemListConfigurator configurator, CriteriaListUI listUI) {
        super(configurator, listUI);
    }

    @Override
    public String[] getComboItems() {
        return configurator.getActionNames();
    }

    @Override
    protected String getPageId() {
        return "actionWizardPage";
    }

    @Override
    protected void createBuilderPanelContent() {
        type = ActionType.valueOf(combo.getItem(combo.getSelectionIndex()).toUpperCase());
        checkbox = null;
        text2 = null;
        Composite textFieldPanel = null;

        if (type == ActionType.EXTENSION_REPLACE) {
            textFieldPanel = createTextFieldPanel("extension: ");
            addHelpLabel(textFieldPanel, getExampleText());
        }
        else if (type == ActionType.SUFFIX_APPEND) {
            textFieldPanel = createTextFieldPanel("suffix: ");
            addHelpLabel(textFieldPanel, getExampleText());
            addInverseCheckbox();
        }
        else if (type == ActionType.PREFIX_APPEND) {
            textFieldPanel = createTextFieldPanel("prefix: ");
            addHelpLabel(textFieldPanel, getExampleText());
            addInverseCheckbox();
        }
        else if (type == ActionType.FILE_SAVE) {
            textFieldPanel = createTextFieldPanel("key: ");
            addHelpLabel(textFieldPanel, getExampleText());
        }
        else if (type == ActionType.FILE_LOAD) {
            textFieldPanel = createTextFieldPanel("key: ");
            addHelpLabel(textFieldPanel, getExampleText());
        }
        else if (type == ActionType.LOCATION_REPLACE || type == ActionType.FILENAME_REPLACE) {
            textFieldPanel = createTextFieldPanel("match: ");
            UITools.addLabel(textFieldPanel, "replace: ");
            text2 = UITools.addTextField(textFieldPanel);
            addHelpLabel(textFieldPanel, getExampleText());
        }
        Listener buttonRefreshListener = new Listener() {
            public void handleEvent(Event arg0) {
                updateButtons();
            }
        };
        if (text2 != null) {
            text2.addListener(SWT.Modify, buttonRefreshListener);
        }
    }

    @Override
    protected String getCheckboxText() {
        return "inverse this action";
    }

    @Override
    public boolean performFinish() {
        String str1 = null;
        String str2 = null;
        str1 = text.getText();
        if (text2 != null) {
            str2 = text2.getText();
        }
        boolean inverseAction = checkbox != null && checkbox.getSelection();
        configurator.createAction(type, str1, str2, inverseAction);
        listUI.updateListItems();
        listUI.updateButtons();
        return true;
    }

    @Override
    protected boolean isWizardComplete() {
        return isText1OK();
    }

    private boolean isText1OK() {
        return text != null && text.getText().trim().length() > 0;
    }

    @Override
    protected String getPageDescription() {
        return HelpTexts.getActionWizardTitle();
    }

    @Override
    protected String getTitle() {
        return "Add Action";
    }

    @Override
    protected Enum<?> getTypeForItemName(String itemName) {
        return ActionType.valueOf(itemName.toUpperCase());
    }
}
